/**
 * 
 */
package com.thinkgsp.jxc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgsp.jxc.model.ProductBatchNo;
import com.thinkgsp.jxc.persistence.ProductBatchNoMapper;
import com.thinkgsp.jxc.service.ProductBatchNoService;
import com.thinkgsp.jxc.service.ServiceException;

/**
 * @author evan
 *
 */
@Service("batchNoService")
@Transactional
public class ProductBatchNoServiceImpl extends GeneralServiceImpl implements
		ProductBatchNoService {

	@Autowired
	private ProductBatchNoMapper batchNoMapper;
	
	public void setBatchNoMapper(ProductBatchNoMapper batchNoMapper) {
		this.batchNoMapper = batchNoMapper;
	}
	
	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.ProductBatchNoService#getBatchNoById(java.lang.Long)
	 */
	public ProductBatchNo getBatchNo(Long id) {
		return batchNoMapper.getBatchNoById(id);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.ProductBatchNoService#getBatchNoByProduct(java.lang.Integer, java.lang.String)
	 */
	public ProductBatchNo getBatchNo(Integer productId, String batchNo) {
		return batchNoMapper.getBatchNoByProduct(productId, batchNo);
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.ProductBatchNoService#saveBatchNo(com.thinkgsp.jxc.model.ProductBatchNo)
	 */
	public void saveBatchNo(ProductBatchNo batchNo) {
		if(batchNo.getId() == null || batchNo.getId() == 0L) {
			batchNoMapper.insertBatchNo(batchNo);
		} else {
			batchNoMapper.updateBatchNo(batchNo);
		}
	}

	/* (non-Javadoc)
	 * @see com.thinkgsp.jxc.service.ProductBatchNoService#deleteBatchNo(java.lang.Long)
	 */
	public void deleteBatchNo(Long id) {
		batchNoMapper.deleteBatchNo(id);
	}

	public List<ProductBatchNo> getBatchNo(Integer productId) {
		return batchNoMapper.getBatchNoByProductId(productId);
	}

	public ProductBatchNo getNonBatchNo(Integer productId) throws ServiceException {
		try {
			ProductBatchNo batchNo = getBatchNo(productId, ProductBatchNo.NON_BATCH_MGR_VALUE);
//			if(batchNo == null) {
//				throw new ServiceException("本产品的空批号不存在!");
//			}
			return batchNo;
		} catch(Exception e) {
			log.error(e);
			throw new ServiceException("本产品的空批号不存在!");
		}
	}

}
